/**
 * 初始化采购单管理详情对话框
 */
var PurchaseInfoDlg = {
    purchaseInfoData : {}
};

/**
 * 清除数据
 */
PurchaseInfoDlg.clearData = function() {
    this.purchaseInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
PurchaseInfoDlg.set = function(key, val) {
    this.purchaseInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
PurchaseInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
PurchaseInfoDlg.close = function() {
    parent.layer.close(window.parent.Purchase.layerIndex);
}

/**
 * 收集数据
 */
PurchaseInfoDlg.collectData = function() {
    this
    .set('id')
    .set('purId')
    .set('purFax')
    .set('purPic')
    .set('purPicTel')
    .set('purSupplier')
    .set('purSupplierTel')
    .set('purAdress')
    .set('purPayType')
    .set('purDate')
    .set('purNumber')
    .set('purName')
    .set('purSpec')
    .set('purState');
}

/**
 * 提交添加
 */
PurchaseInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/purchase/add", function(data){
        console.log(data);
        if(data.code==200){
            Feng.success("添加成功!");
            window.parent.Purchase.table.refresh();
            PurchaseInfoDlg.close();
        }else{
            Feng.error(data.message);
        }

    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.purchaseInfoData);
    ajax.start();
}


/**
 * 提交修改
 */
PurchaseInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/purchase/update", function(data){
        Feng.success("修改成功!");
        window.parent.Purchase.table.refresh();
        PurchaseInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.purchaseInfoData);
    ajax.start();
}

/**
 * 提交采购状态
 */
PurchaseInfoDlg.stateSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/purchase/state", function(data){
        Feng.success("修改成功!");
        window.parent.Purchase.table.refresh();
        PurchaseInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.purchaseInfoData);
    ajax.start();
}

/**
 *  获取采购名称
 */
PurchaseInfoDlg.getPurName= function () {
    var ajax = new $ax(Feng.ctxPath + "/purchase/purName", function (data) {
        if(data.length>0){
            $("#purName").empty();
            var option = $("<option>").val("").text("----请选择报买名称----");
            $("#purName").append(option);
            for(var i=0;i<data.length;i++){
                var option = $("<option>").val(data[i]).text(data[i]);
                $("#purName").append(option);
            }
        }
    }, function (data) {
        Feng.error("网络错误!");
    });
    ajax.set("type",0);
    ajax.start();
};

/**
 *  获取供应商名称
 */
PurchaseInfoDlg.getPurSupplier= function () {
    var ajax = new $ax(Feng.ctxPath + "/purchase/purSupplier", function (data) {
        if(data.length>0){
            $("#purSupplier").empty();
            var option = $("<option>").val("").text("----请选择供应商名称----");
            $("#purSupplier").append(option);
            for(var i=0;i<data.length;i++){
                var option = $("<option>").val(data[i]).text(data[i]);
                $("#purSupplier").append(option);
            }
        }
    }, function (data) {
        Feng.error("网络错误!");
    });
    ajax.set("type",0);
    ajax.start();
};

/*立即执行函数$(function() {
});*/
$(function() {
    PurchaseInfoDlg.getPurName();
    PurchaseInfoDlg.getPurSupplier();
});
